package main.java.cn.wolfcode.web.controller;


import cn.hutool.core.date.BetweenFormater;
import cn.hutool.core.date.DateUtil;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.enums.NoticeEnum;
import cn.wolfcode.qo.NoticeQueryObject;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;


    @RequiredPermission(name="公告页面",expression="notice:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") NoticeQueryObject qo){
        PageInfo<Notice> pageInfo = noticeService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("enums", NoticeEnum.values());

        //拿到当前用户的全部公告条数
        List<Long> list = noticeService.selectNoticeCountByEmpId(UserContext.getCurrentUser().getId());
        //拿到所有的公告
        List<Long> longs = noticeService.listAll();
        List<Long> longList =new ArrayList<>();
        //不包含则表示是未读的,放到存放到集合中
        for (Long aLong : longs) {
           if (!list.contains(aLong)){
               longList.add(aLong);
           }
        }
        List<Notice> notices=noticeService.selectNOLook(list);
        for (Notice notice : notices) {
            String s = DateUtil.formatBetween(notice.getCreatetime(), new Date(), BetweenFormater.Level.MINUTE);
            notice.setTimeDifference(s);
        }
        UserContext.getSession().setAttribute("notices",notices);

        /*遍历公告查看那些公告是未读的
        for (Notice notice : list) {
            if(notice.isReadr()){
                int count= noticeService.selectUnread(notice.isReadr());
                model.addAttribute("count",count);
            }
        }*/
        return "notice/list";
    }


    @RequiredPermission(name="公告删除",expression="notice:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           noticeService.delete(id);
        }
        return "redirect:/notice/list";
    }


    @RequiredPermission(name="公告新增/编辑",expression="notice:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Notice notice,Long[] ids) {
        if (notice.getId() == null) {
            notice.setFounder(UserContext.getCurrentUser().getName());
            notice.setCreatetime(new Date());
            noticeService.save(notice,ids);
            notice.setStatus(true);
            notice.setReadr(true);
        } else {
            noticeService.update(notice,ids);
        }
        return "redirect:/notice/list";
    }


    @RequiredPermission(name="公告新增/编辑",expression="notice:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("notice",noticeService.get(id));
        }
        return "notice/input";
    }
    @RequiredPermission(name="公告状态更改",expression="notice:updateStatus")
    @RequestMapping("/updateStatus")
    public String updateStatus(Long id, Integer status) {
       noticeService.updateStatus(id,status);
        return "redirect:/notice/list";
    }

    @RequiredPermission(name="公告查看",expression="notice:show")
    @RequestMapping("/show")
    public String show() {
        return "/notice/show";
    }

    @RequiredPermission(name="查看状态更改",expression="notice:updateReadr")
    @RequestMapping("/updateReadr")
    public String updateReadr(Long id, Integer readr) {
        //查询出所有的公告条数
        List<Long> ids = noticeService.listAll();
        //查询出员工所查看过的公告
        List<Notice> list = noticeService.selectNoticeByEmpId(UserContext.getCurrentUser().getId());

        for (Notice notice : list) {
            if (ids.contains(notice.getId())){
                System.out.println(notice.getId());
                //表示已读,设置状态为已读
                noticeService.updateReadr(notice.getId(),readr);
            }
        }
        return "redirect:/notice/list";
    }

    @RequiredPermission(name="未读公告查看",expression="notice:selectUnread")
    @RequestMapping("/selectUnread")
    public String selectUnread(Model model) {

        return "/notice/list";
    }
}
